package org.example.service;

import org.example.dto.CategoryDTO;
import org.example.dto.OPtionsDTO;
import org.example.dto.SpecDTO;
import org.example.entity.Category;
import org.example.entity.Spec;
import org.example.mapper.Shanpinguigemapper;
import org.example.vo.SpecVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShanpinguigeService {
    @Autowired
    Shanpinguigemapper shanpinguigemapper;


    /**
     * 商品规格查询
     * @param username
     * @return
     */
    public List<Category> cx(String username) {
        List<Category> category=shanpinguigemapper.cx(username);
        for (Category c : category) {
            List<SpecVO> specVO=shanpinguigemapper.cxguigezhi(c.getId());
            List<Spec> specs=new ArrayList<>();
            for(SpecVO s:specVO){

                List<String> strings=shanpinguigemapper.cxoption(s.getId());


                Spec spec=new Spec();
                spec.setOptions(strings);
                spec.setName(s.getSpecName());
                specs.add(spec);
            }
            c.setSpecs(specs);
        }
        return category;
    }

    /**
     * 新增分类
     * @param
     */
    public void xzfele(CategoryDTO categoryDTO) {
        shanpinguigemapper.xzfele(categoryDTO);
    }

    /**
     * 删除分类
     * @param username
     * @param id
     */
    public void scfele(String username, String id) {
       List<Integer>guizhiid=shanpinguigemapper.sccx(username,id,null);
        shanpinguigemapper.scfele(username,id);
        shanpinguigemapper.scgui(username,id,null);
        for(Integer g:guizhiid){
            shanpinguigemapper.scguizhis(g);
        }
    }

    /**
     * 新增规格
     * @param specDTO
     */
    public void xzgui(SpecDTO specDTO) {
        shanpinguigemapper.xzgui(specDTO);
    }

    /**
     * 删除规格
     * @param username
     * @param id
     */
    public void scgui(String username, String id,String name) {
        List<Integer> specid=shanpinguigemapper.sccx(username,id,name);
        shanpinguigemapper.scgui(username,id,name);
        shanpinguigemapper.scguizhis(specid.get(0));
    }

    /**
     * 规格值新增
     * @param oPtionsDTO
     */
    public void xzguizhi(OPtionsDTO oPtionsDTO) {
        oPtionsDTO.setId(shanpinguigemapper.cxguige(oPtionsDTO));
        shanpinguigemapper.xzguizhi(oPtionsDTO);
    }

    /**
     * 删除规格值
     *
     * @param name
     * @param id
     * @param specname
     * @param username
     */
    public void scguizhi(String name, String id, String specname, String username) {
        int specid=shanpinguigemapper.chaxuguigezhi(id,specname,username);
        shanpinguigemapper.scguizhi(name,specid);
    }


}
